import os
import yaml
from config.conf import YAML_DIR
class YamlTools:
    @classmethod
    def read_yaml_file(self,filename):
        filename=YAML_DIR + "\\"+ filename
        with open(filename,'r',encoding="utf-8") as file:
            content = file.read()
            #转换yaml格式 为列表 或者 字典
            result = yaml.load(content,Loader=yaml.FullLoader)
        return result
    @classmethod
    def write_yaml_file(self,filename,data):
        # 创建一个yaml 文件得存放目录 /
        path = YAML_DIR
        if not os.path.exists(path):
            os.makedirs(path)
        filename = YAML_DIR + filename
        with open(filename,'w',encoding="utf-8") as file:
            yaml.dump(data,file,allow_unicode=True)
if __name__ == '__main__':
    # testdata = [{"username":"123456","password":"123456","expect":"签退","type":"正向"},
    #             {"username":"","password":"123456","expect":"登录失败，请检查您的成员名或密码是否填写正确。","type":"反向"},
    #             {"username":"123456","password":"","expect":"登录失败，请检查您的成员名或密码是否填写正确。","type":"反向"}
    #             ]
    testdata=[{"username":"","name":"fefefefeffefefe","mm":"123456","MM":"123456","num":"59624687292842654@qq.com","expect":"用户名不能为空。;用户名应当为字母或数字的组合，至少三位","expect1":"编辑","type":"反向"}]
    YamlTools.write_yaml_file("\\test.yaml",testdata)
    # print(YamlTools.read_yaml_file("test.yaml"))